﻿// -----------------------------------------------------------------------
//    This file is part of TSGE.
//
//    TSGE is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    TSGE is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with TSGE.  If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------

namespace tsge.Classes
{
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Xml.Serialization;

    /// <summary>
    /// Buff Class Implementation
    /// 
    ///     Inherits:
    ///         - ICloneable
    ///         - INotifyPropertyhanged
    /// </summary>
    [Serializable]
    public class Buff : ICloneable, INotifyPropertyChanged
    {
        /// <summary>
        /// Internal copy of the buff id.
        /// </summary>
        private Int32 m_vBuffId;

        /// <summary>
        /// Internal copy of the buff icon uri.
        /// </summary>
        private Uri m_vBuffIcon;

        /// <summary>
        /// Internal copy of the buff name.
        /// </summary>
        private String m_vBuffName;

        /// <summary>
        /// Internal copy of the buff tooltip.
        /// </summary>
        private String m_vBuffTooltip;

        /// <summary>
        /// Internal copy of the buff duration.
        /// </summary>
        private Int32 m_vBuffDuration;

        /// <summary>
        /// Internal copy of the buff duration string.
        /// </summary>
        private String m_vBuffString;

        /// <summary>
        /// Public PropertyChanged event to register onto.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public Buff()
        {
        }

        /// <summary>
        /// Invokes the PropertyChanged event if one is set.
        /// </summary>
        /// <param name="propName">Name of the property being updated.</param>
        private void NotifyPropertyChanged(String propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        /// <summary>
        /// Gets or sets this buffs id.
        /// </summary>
        [XmlAttribute("index")]
        public Int32 Id
        {
            get { return this.m_vBuffId; }
            set
            {
                this.m_vBuffId = value;
                this.Icon = new Uri(String.Format(@"{0}\Data\Buffs\buff_{1}.png", AppDomain.CurrentDomain.BaseDirectory, value), UriKind.Absolute);
                this.NotifyPropertyChanged("Id");
            }
        }

        /// <summary>
        /// Gets or sets this buffs name.
        /// </summary>
        [XmlAttribute("name")]
        public String Name
        {
            get { return this.m_vBuffName; }
            set { this.m_vBuffName = value; this.NotifyPropertyChanged("Name"); }
        }

        /// <summary>
        /// Gets or sets this buffs tooltip.
        /// </summary>
        [XmlAttribute("tooltip")]
        public String Tooltip
        {
            get { return this.m_vBuffTooltip; }
            set { this.m_vBuffTooltip = value; this.NotifyPropertyChanged("Tooltip"); }
        }

        /// <summary>
        /// Gets or sets this buffs duration.
        /// </summary>
        [XmlAttribute("duration")]
        public Int32 Duration
        {
            get { return this.m_vBuffDuration; }
            set
            {
                this.m_vBuffDuration = value;
                this.DurationString = "Refreshing...";
                this.NotifyPropertyChanged("Duration");
            }
        }

        /// <summary>
        /// Gets or sets this buffs icon.
        /// </summary>
        [XmlIgnore]
        public Uri Icon
        {
            get { return this.m_vBuffIcon; }
            set { this.m_vBuffIcon = value; this.NotifyPropertyChanged("Icon"); }
        }

        /// <summary>
        /// Gets or sets this buffs duration string.
        /// </summary>
        [XmlIgnore]
        public String DurationString
        {
            get { this.m_vBuffString = String.Format("{0} seconds", this.Duration / 60); return this.m_vBuffString; }
            set { this.m_vBuffString = String.Format("{0} seconds", this.Duration / 60); this.NotifyPropertyChanged("DurationString"); }
        }

        /// <summary>
        /// Updates this buff entry to the given buff.
        /// </summary>
        /// <param name="nBuffId">The buff id to set this buff instance to.</param>
        public void SetBuff(Int32 nBuffId)
        {
            var buff = (from Buff b in Terraria.Instance.Buffs
                        where b.Id == nBuffId
                        select b).SingleOrDefault();
            
            this.Duration = buff.Duration;
            this.Icon = new Uri(String.Format(@"{0}\Data\Buffs\buff_{1}.png", AppDomain.CurrentDomain.BaseDirectory, buff.Id), UriKind.Absolute);
            this.Id = buff.Id;
            this.Name = buff.Name;
            this.Tooltip = buff.Tooltip;
        }

        /// <summary>
        /// ToString Override
        /// </summary>
        /// <returns>Returns the name of this buff.</returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// ICloneable Implementation
        /// </summary>
        /// <returns>Returns a memberwise clone of this object.</returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}
